package com.lcp.se.entity.result;

import com.lcp.se.util.StatusCode;

import java.io.Serializable;

/**
 * 封装响应结果
 *
 * @author 李昌平
 * @date 2019/11/3 20:50
 */
public class Result<T> implements Serializable {
    /**
     * 返回码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 操作是否成功，也可以通过code判断
     */
    private boolean flag;

    public Result() {
    }

    public Result(boolean flag, Integer code) {
        this.flag = flag;
        this.code = code;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result(boolean flag, Integer code, String message, T data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /*
     * 下面是一些快捷调用
     * */

    // 成功，不包含数据，不包含消息
    public static <T> Result<T> success() {
        return new Result<>(true, StatusCode.OK);
    }

    // 成功，不包含数据
    public static <T> Result<T> success(String message) {
        return new Result<>(true, StatusCode.OK, message);
    }

    // 成功，包含数据
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(true, StatusCode.OK, message, data);
    }

    // 失败，不包含数据，不包含消息
    public static <T> Result<T> failure() {
        return new Result<>(false, StatusCode.ERROR);
    }

    // 失败，不包含数据
    public static <T> Result<T> failure(String message) {
        return new Result<>(false, StatusCode.ERROR, message, null);
    }

    // 失败，包含数据
    public static <T> Result<T> failure(String message, T data) {
        return new Result<>(false, StatusCode.ERROR, message, data);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", flag=" + flag +
                '}';
    }
}

